package com.ihrm.company.controller;


import com.ihrm.common.entity.Result;
import com.ihrm.common.entity.ResultCode;
import com.ihrm.common.exception.CommonException;
import com.ihrm.company.service.CompanyService;
import com.ihrm.domain.company.Company;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author chahua
 * @date 2022/3/26 15:30
 **/
@CrossOrigin
@Api(value = "企业管理增删改查接口")
@RestController
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    /**
     * @RequestBody作用是将前端请求的json封装成Company对象,前提是属性名相同
     */
    @ApiOperation("新增企业")
    @PostMapping("/company")
    public Result save(@RequestBody Company company){
        companyService.add(company);
        return new Result(ResultCode.SUCCESS);
    }

    @ApiOperation("更新企业")
    @PatchMapping("/company/{id}")
    public Result update(@PathVariable("id") String id,Company company){
        company.setId(id);
        companyService.update(company);
        return new Result(ResultCode.SUCCESS);
    }

    @ApiOperation("删除企业")
    @DeleteMapping("/company/{id}")
    public Result delete(@PathVariable("id") String id){
        companyService.deleteById(id);
        return new Result(ResultCode.SUCCESS);
    }

    @ApiOperation("根据企业id查询企业")
    @GetMapping("company/{id}")
    public Result getById(@PathVariable("id") String id) throws CommonException {
        Company company = companyService.findById(id);
        return new Result(ResultCode.SUCCESS,company);
    }

    @ApiOperation("查询企业列表")
    @GetMapping("/company")
    public Result getAll() throws CommonException {
        List<Company> companyList = companyService.findAll();
        return new Result(ResultCode.SUCCESS,companyList);
    }
}
